package com.xpgk.beetl.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.beetl.core.Tag;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.plugin.ParamInitPlugin;

/**
 * 参数输出标签：支持select、radio、checkbox
 * 
 */
public class CodeTag extends Tag {

	private static final Log log = Log.getLog(CodeTag.class);

	@SuppressWarnings("unchecked")
	@Override
	public void render() {
		try {
			Map<String, String> param = (Map<String, String>) args[1];

			if(log.isDebugEnabled()) log.debug("获取标签属性");

			String type = param.get("type") == null ? "" : param.get("type");
			
			String id = param.get("id") == null ? "" : param.get("id");
			String name = param.get("name") == null ? "" : param.get("name");
			
			String class_ = param.get("class") == null ? "" : param.get("class");
			String style = param.get("style") == null ? "" : param.get("style");

			String code = param.get("code") == null ? "" : param.get("code");
			String defaultCode = param.get("defaultCode") == null ? "" : param.get("defaultCode");
			String isSelectDef = param.get("isSelectDef") == null ? "true" : param.get("isSelectDef");
			String dataOptions = param.get("dataOptions") == null ? "" : param.get("dataOptions");

			
			if(type.equals("")){
				ctx.byteWriter.writeString(select(id, name, class_, style, code, defaultCode, isSelectDef, dataOptions));
				
			}else if(type.equals("select")){
				ctx.byteWriter.writeString(select(id, name, class_, style, code, defaultCode, isSelectDef, dataOptions));
				
			}else if(type.equals("select_val")){
				ctx.byteWriter.writeString(select_val(id, name, class_, style, code, defaultCode, dataOptions));
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下拉标签 value=ids
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param number
	 * @param defaultnumber
	 * @return
	 */
	private String select(String id, String name, String class_, String style, String code, String defaultCode, String isSelectDef, String dataOptions){
		StringBuilder sb = new StringBuilder();
		if (StrKit.notBlank(id)) {
			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" data-options=\"").append(dataOptions).append("\" >");
		} else {
			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
			sb.append("\" style=\"").append(style).append("\" data-options=\"").append(dataOptions).append("\" >");
		}

		Code parentCode = Code.cacheGet(Code.getCode(code));
		List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());
		sb.append("<option value='-1'>-请选择-</option>");
		for (Code c : codeList) {
			String status = c.getStr(Code.column_status);
			String namesTemp = c.getStr(Code.column_name);
			String isDefault = c.getStr(Code.column_isDefault);
			String ids = c.getPKValue();
			if(null == status || status.equals("0")){
				continue;
			}
			if (null != defaultCode && null != ids && defaultCode.equals(ids)) {// 默认选中
				sb.append("<option value=\"").append(ids).append("\" selected=\"selected\">");
				sb.append(namesTemp);
				sb.append("</option>");

			} else {
				if("1".equals(isDefault) && "true".equals(isSelectDef)){
					sb.append("<option value=\"").append(ids).append("\" selected=\"selected\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}else{
					sb.append("<option value=\"").append(ids).append("\">");
					sb.append(namesTemp);
					sb.append("</option>");
				}
			}
		}
		sb.append("</select>");
		
		return sb.toString();
	}
	
	/**
	 * 下拉标签 value=code值
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param number
	 * @param defaultnumber
	 * @return
	 */
	private String select_val(String id, String name, String class_, String style, String code, String defaultCode, String dataOptions){
		StringBuilder sb = new StringBuilder();
		if (StrKit.notBlank(id)) {
			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" data-options=\"").append(dataOptions).append("\" >");
		} else {
			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
			sb.append("\" style=\"").append(style).append("\" data-options=\"").append(dataOptions).append("\" >");
		}
		sb.append("<option value='-1'>-请选择-</option>");
		Code parentCode = Code.cacheGet(Code.getCode(code));
		List<Code> codeList = Code.cacheGetChild(parentCode.getPKValue());

		for (Code c : codeList) {
			String status = c.getStr("status");
			String namesTemp = c.getStr("name");
			String dmz = c.getStr("code");
			String ids = c.getPKValue();
			if(null == status || status.equals("0")){
				continue;
			}
			if (null != defaultCode && null != ids && defaultCode.equals(namesTemp)) {// 默认选中
				sb.append("<option value=\"").append(dmz).append("\" selected=\"selected\">");
				sb.append(namesTemp);
				sb.append("</option>");

			} else {
				sb.append("<option value=\"").append(dmz).append("\">");
				sb.append(namesTemp);
				sb.append("</option>");
			}
		}
		sb.append("</select>");
		
		return sb.toString();
	}

}
